<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/8/23 0023
 * Time: 11:47
 */
namespace app\admin\model;
use think\Db;

class PositionModel
{
    /**获取职位模型
     * @return $this
     */
    static public function getPosition(){
        return Db::view('position_table',true)
            ->view('company','company_name','company.id=position_table.company_id','left')
            ->view('departmen_table','departmen_name,number','departmen_table.id=position_table.department_id','left');
    }

    /**添加职位
     * @param $data
     * @return int|string
     */
    static public function addPosition($data){
        return Db::name('position_table')->insert($data);
    }

    /**删除职位
     * @param $field
     * @param null $op
     * @param null $condition
     * @return int
     */
    static public function delPosition($field, $op = null, $condition = null){
        $sul = Db::connect();
        $positions = $sul->name('position_table')->where($field,$op,$condition)->select();
        foreach ($positions as $position){
            $id = $position['id'];
            $user = $sul->name('user')->where('post_id',$id)->select();
            foreach ($user as $value){
                $userid = $value['id'];
                $sul->name('punch_clock')->where('userid',$userid)->delete();
                $sul->name('leave')->where('userid',$userid)->delete();
                $sul->name('work_report')->where('author',$userid)->delete();
                $sul->name('integral_change')->where('userid',$userid)->delete();
                $sul->name('fabulous')->where('thumbs_up',$userid)->delete();
                $sul->name('comment')->where('userid',$userid)->delete();
            }
            $sul->name('user')->where('post_id',$id)->delete();
        }
        return $sul->name('position_table')->where($field,$op,$condition)->delete();
    }

    /**更新职位
     * @param $data
     * @return int|string
     */
    static public function updatePosition($data){
        return Db::name('position_table')->update($data);
    }
}